
import AnimationComponent from "../animation/AnimationComponent";
import DragonComponent from "../animation/DragonComponent";
import ResItem from "../res/ResItem";
import StringHelper from "../tools/StringHelper";

export default class CCNodePool {

    private pool: cc.NodePool;

    private resItem: ResItem;

    private name: string = ''

    // private getCount:number = 0;

    // private putCount:number = 0;

    /**
     * 
     * @param prefab 预制体
     * @param conut 初始化个数
     */
    constructor(name: string, resItem: ResItem, conut: number) {
        this.name = StringHelper.laststring(name,'/');
        this.pool = new cc.NodePool();
        this.resItem = resItem;
        for (let i = 0; i < conut; i++) {
            let obj: cc.Node = this.getNode(); // 创建节点
            this.pool.put(obj); // 通过 putInPool 接口放入对象池
        }
    }

    getName(){
        return this.name
    }

    get() {
        // let used = this.getCount - this.putCount;
        // if( used > 50){
            // cc.warn(' this.getCount is ',this.getCount,' putCount ',this.putCount," name ",this.name )
        // }
        // this.getCount ++;
        
        let go: cc.Node = this.pool.size() > 0 ? this.pool.get() : this.getNode();
        // cc.log(' get count ',this.pool.size(),go.name )
        return go;
    }

    getNode() {
        let template = this.resItem.getRes();
        if (template instanceof cc.Prefab) {
            return cc.instantiate(template);
        } else if (template instanceof cc.SpriteAtlas) {

            return this.createCocosAnim();
        } else {
            return this.createDragonAnim()
        }
    }

    createCocosAnim(): cc.Node {
        let atlas: cc.SpriteAtlas = this.resItem.getRes();
        let node = new cc.Node();
        node.name = this.name;
        let comp: AnimationComponent = node.addComponent(AnimationComponent)
        if (comp) {
            comp.setActionName(this.name);
            comp.setAutoPlay(false)
            comp.setAtlas(atlas)
            comp.init();
        }
        return node;
    }

    createDragonAnim() {
        let node = new cc.Node();
        node.name = this.name;
        let comp = node.addComponent(DragonComponent);
        if (comp) {
            comp.setAutoPlay(false)
            let template = this.resItem.getRes();
            for (let index = 0; index < template.length; index++) {
                const element = template[index];
                if (element instanceof dragonBones.DragonBonesAsset) {
                    comp.setBonesAsset(element);
                }
                if (element instanceof dragonBones.DragonBonesAtlasAsset) {
                    comp.setAtlas(element)
                }
            }
            comp.init();
        }
        return node;
    }

    size() {
        return this.pool.size();
    }

    put(go: cc.Node) {
        // this.putCount ++;
        // cc.log(' put ',go.name)
        this.pool.put(go);
        // cc.log(' put  count ',this.pool.size(),go.name )
    }

    clear() {
        this.pool.clear();
    }

}
